<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  <meta content="yes" name="apple-mobile-web-app-capable">
  <meta content="black" name="apple-mobile-web-app-status-bar-style">
  <meta content="telephone=no" name="format-detection">
  <title>获取unionId</title>
  <meta name="description" content="" />
  <link rel="shortcut icon" href="https://www.svenglish.cn/images/favicon.ico" type="image/x-icon">
  <link rel="stylesheet" href="css/common.css">
  <style>
    .outLine-tit {
      font-size: 0.3rem;
      line-height: 0.3rem;
      text-align: center;
      color: #242424;
    }

    .home-form {
      padding: 0.5rem 0.3rem;
      margin: 0.3rem 0.3rem 0.8rem;
      box-sizing: border-box;
      box-shadow: 0 0 0.2rem rgba(0, 0, 0, 0.1);
      border-radius: 0.2rem;
    }

    .home-form p {
      padding: 0.5rem 0;
      text-align: center;
    }
  </style>
</head>

<body>
  <div class="page_all" id="outLinePage">
    <div class='header'>
      <a class='logo' href="index.html"><img src='images/logo.png'></a>
    </div>

    <div class="home-form">
      <h3 class="outLine-tit">您的Unionid为</h3>
      <p id="copyText">{{unionId}}</p>
      <div class="">
        <span class="btn" onclick="oneKeyCopy(document.getElementById('copyText'));">一键复制</span>
      </div>
    </div>
  </div>

  <div role="alert" class="el-alert el-alert--success js_elAlert">
    <div class="el-alert__content"><span class="el-alert__title">复制成功，可以去分享啦</span></div>
  </div>

  <script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
  <script src="js/vue.min.js"></script>
  <script src="js/zepto.min.js"></script>
  <script>
    new Vue({
      el: '#outLinePage',
      data() {
        return {
          unionId: ''
        }
      },
      methods: {
        isWeiXin() {
          var ua = window.navigator.userAgent.toLowerCase();
          if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            return true;
          } else {
            return false;
          }
        },
      },
      mounted() {
        var wxUserInfo = localStorage.getItem('wxUserInfo');
        if (wxUserInfo) {
          var wxUserInfo = JSON.parse(wxUserInfo);
          this.unionId = wxUserInfo.unionId;
          if (!this.unionId && this.isWeiXin()) {
            location.href = 'login.html?url=' + encodeURIComponent(location.href.split('#')[0]);
          }
        } else if (this.isWeiXin() && !wxUserInfo) {
          location.href = 'login.html?url=' + encodeURIComponent(location.href.split('#')[0]);
        }
      }

    });

    // 一键copy
    function selectText(x) {
      if (document.selection) {
        var range = document.body.createTextRange();
        range.moveToElementText(x);
        range.select();
      } else if (window.getSelection) {
        var selection = window.getSelection();
        var range = document.createRange();
        selection.removeAllRanges();
        range.selectNodeContents(x);
        selection.addRange(range);
      }
    }

    let $elAlert = $('.js_elAlert');
    function oneKeyCopy(x) {
      selectText(x);
      document.execCommand("copy");
      $elAlert.addClass('show');
      setTimeout(() => {
        $elAlert.addClass('hide');
        setTimeout(() => {
          $elAlert.removeClass('show').removeClass('hide');
        }, 500);
      }, 1000);
    }
  </script>
</body>

</html>